package mysql

import (
	"home-view/homerpc/homerpc"

	"gorm.io/gorm"
)

type Collect struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11);not null;comment:'用户ID'"`
	ClassId int64 `gorm:"type:int(11);not null;comment:'课程ID'"`
}

func (c *Collect) CollectAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Collect) FindCollectById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(c).Error
}

func (c *Collect) CollectList(db *gorm.DB, id int64, lists []*homerpc.CollectList) ([]*homerpc.CollectList, error) {
	/*
		SELECT
		classes.class_name,classes.price,classes.class_type,classes.content
		FROM
		collects
		LEFT JOIN users ON users.id=collects.user_id
		LEFT JOIN classes on classes.id=collects.class_id
		WHERE users.id =1
	*/
	err := db.Debug().Model(c).Select("classes.class_name,classes.price,classes.class_type,classes.content").
		Joins("LEFT JOIN users ON users.id=collects.user_id").
		Joins("LEFT JOIN classes on classes.id=collects.class_id").
		Where("users.id = ?", id).
		Order("classes.created_at desc").
		Find(&lists).Error
	return lists, err
}

func (c *Collect) CollectDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(c).Error
}
